package com.tgy.JUC.BlockingQueueDemo;

import com.sun.corba.se.impl.orbutil.concurrent.Sync;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/***
 * @ClassName: Test5
 * @Description: SynchronousQueue测试 没有大小，进一个出一个
 * @Auther: 送你一块二向箔
 * @Date: 10:08 2022/3/14
 * @version : V1.0
 */
public class Test5 {
    public static void main(String[] args) {
        run2();
    }

    public static void run() {
        SynchronousQueue<String> strings = new SynchronousQueue<>();
        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+" put 1");
                strings.put("1");
                TimeUnit.SECONDS.sleep(2L);
                System.out.println(Thread.currentThread().getName()+" put 2");
                strings.put("2");
                TimeUnit.SECONDS.sleep(2L);
                System.out.println(Thread.currentThread().getName()+" put 3");
                strings.put("3");
                TimeUnit.SECONDS.sleep(2L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T1").start();


        new Thread(()->{
            try {
                System.out.println("T2 ====="+strings.take());
                TimeUnit.SECONDS.sleep(4L);
                System.out.println("T2 ====="+strings.take());
                TimeUnit.SECONDS.sleep(4L);
                System.out.println("T2 ====="+strings.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"T2").start();
    }

    public static void run2(){
        SynchronousQueue<String> strings = new SynchronousQueue<>();
        try {
            strings.put("1");
            System.out.println("====================");
            TimeUnit.SECONDS.sleep(3L);
            strings.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
